import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mall/constant/app_dimens.dart';
import 'package:mall/modules/sc/goods_sc.dart';
import 'package:mall/ui/page/goods/widgets/goods_widget.dart';
import 'package:mall/utils/list_placeholder_component/list_placeholder_component.dart';
import 'package:mall/router/navigator_util.dart';
import 'package:mall/utils/refresh/refresh_widget.dart';
import 'package:mall/utils/refresh/warp_refresh_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CategoryGoodsWidget extends StatefulWidget {
  final int _categoryId;

  CategoryGoodsWidget(this._categoryId);

  @override
  _CategoryGoodsWidgetState createState() => _CategoryGoodsWidgetState();
}

class _CategoryGoodsWidgetState extends State<CategoryGoodsWidget> {
  // int _categoryId;
  // CategoryGoodsViewModel _categoryGoodsViewModel = CategoryGoodsViewModel();
  // int _pageIndex = 1;
  // int _pageSize = 6;
  // final sc_categoryTitle = Get.put(CategoryTitleSc());
  final sc_categoryGoodsList = Get.put(CategoryGoodsListSc());
  late final _refreshController = WarpRefreshController(loadState: sc_categoryGoodsList.loadState);

  @override
  void initState() {
    super.initState();
    // _categoryId = widget._categoryId;
    // _categoryGoodsViewModel.queryCategoryGoods(
    //     _categoryId, _pageIndex, _pageSize);
    // sc_categoryTitle.categoryId = widget._categoryId;
    sc_categoryGoodsList.categoryId = widget._categoryId;
    // sc_categoryTitle.refreshData();
    // sc_categoryGoodsList.refreshData();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(
        left: 15,
        right: 15,
      ),
      child: _contentView(),
    );
  }

  // Widget showWidget(CategoryGoodsViewModel categoryGoodsViewModel) {
  //   if (categoryGoodsViewModel.pageState == PageState.hasData) {
  //     return _contentView(categoryGoodsViewModel);
  //   }
  //   return ViewModelStateWidget.stateWidgetWithCallBack(
  //       categoryGoodsViewModel, _onRefresh);
  // }

  Widget _contentView() {
    return Container(
      //     child: SmartRefresher(
      //   enablePullDown: true,
      //   enablePullUp: _categoryGoodsViewModel.canLoadMore,
      //   onRefresh: () => _onRefresh(),
      //   onLoading: () => _onLoadMore(),
      //   header: WaterDropMaterialHeader(
      //     backgroundColor: AppColors.COLOR_FF5722,
      //   ),
      //   controller: _refreshController,
      //   child: GridView.builder(
      //       itemCount: categoryGoodsViewModel.goods.length,
      //       gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      //           crossAxisCount: 2,
      //           mainAxisSpacing: ScreenUtil().setWidth(AppDimens.DIMENS_10),
      //           crossAxisSpacing: ScreenUtil().setHeight(AppDimens.DIMENS_10),
      //           childAspectRatio: 0.8),
      //       itemBuilder: (BuildContext context, int index) {
      //         return GoodsWidget(categoryGoodsViewModel.goods[index], (value) {
      //           NavigatorUtil.goGoodsDetails(context, value);
      //         });
      //       }),
      // ),
      child: Obx(
        () => RefreshComponent(
          controller: _refreshController,
          onLoading: () => sc_categoryGoodsList.loadMoreData(),
          onRefresh: () => sc_categoryGoodsList.refreshData(),
          child: WrappedListPlaceholderComponent.widget(
            sc: sc_categoryGoodsList,
            builder: (p0) => GridView.builder(
                itemCount: p0.length,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    mainAxisSpacing: ScreenUtil().setWidth(AppDimens.DIMENS_10),
                    crossAxisSpacing: ScreenUtil().setHeight(AppDimens.DIMENS_10),
                    childAspectRatio: 0.8),
                itemBuilder: (BuildContext context, int index) {
                  return GoodsWidget(p0[index], (value) {
                    NavigatorUtil.goGoodsDetails(context, value);
                  });
                }),
          ),
        ),
      ),
    );
  }

  // void _onRefresh() {
  //   _pageIndex = 1;
  //   _categoryGoodsViewModel.queryCategoryGoods(
  //       _categoryId, _pageIndex, _pageSize);
  // }

  // void _onLoadMore() {
  //   _categoryGoodsViewModel.queryCategoryGoods(
  //       _categoryId, _pageIndex, _pageSize);
  // }
}
